﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsVote
    {
        private readonly KeRui.DAL.CmsVote dal = new KeRui.DAL.CmsVote();

        public int Add(KeRui.Model.CmsVote model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsVote> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsVote> list = new List<KeRui.Model.CmsVote>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsVote item = new KeRui.Model.CmsVote();
                    if (dt.Rows[i]["VoteID"].ToString() != "")
                    {
                        item.VoteID = int.Parse(dt.Rows[i]["VoteID"].ToString());
                    }
                    if (dt.Rows[i]["SiteID"].ToString() != "")
                    {
                        item.SiteID = int.Parse(dt.Rows[i]["SiteID"].ToString());
                    }
                    if (dt.Rows[i]["VoteType"].ToString() != "")
                    {
                        item.VoteType = int.Parse(dt.Rows[i]["VoteType"].ToString());
                    }
                    if (dt.Rows[i]["VoteLimit"].ToString() != "")
                    {
                        item.VoteLimit = int.Parse(dt.Rows[i]["VoteLimit"].ToString());
                    }
                    item.VoteNum = int.Parse(dt.Rows[i]["VoteNum"].ToString());
                    item.Title = dt.Rows[i]["Title"].ToString();
                    item.Content = dt.Rows[i]["Content"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int VoteID)
        {
            this.dal.Delete(VoteID);
        }

        public bool Exists(int VoteID)
        {
            return this.dal.Exists(VoteID);
        }

        public int GetCount(string Conditions)
        {
            string strWhere = "";
            if (Conditions != "")
            {
                strWhere = " SiteId=" + int.Parse(Conditions);
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsVote", strWhere);
        }

        public List<KeRui.Model.CmsVote> GetList(int PageSize, int PageIndex, string strCondition)
        {
            string strWhere = "";
            if (strCondition != "")
            {
                strWhere = " SiteId=" + int.Parse(strCondition);
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            string strOrder = " VoteID desc";
            DataSet set = common.GetList("CmsVote", PageSize, PageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsVote> GetListByCache(int PageSize, int PageIndex, string strCondition)
        {
            string cacheKey = string.Concat(new object[] { "CmsVoteList-", strCondition, "-", PageSize, "-", PageIndex, "" });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(PageSize, PageIndex, strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsVote>) cache;
        }

        public KeRui.Model.CmsVote GetModel(int VoteID)
        {
            return this.dal.GetModel(VoteID);
        }

        public KeRui.Model.CmsVote GetModelByCache(int VoteID)
        {
            string cacheKey = "CmsVoteModel-" + VoteID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(VoteID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsVote) cache;
        }

        public void Update(KeRui.Model.CmsVote model)
        {
            this.dal.Update(model);
        }

        public void UpdateSum(int VoteID)
        {
            this.dal.UpdateSum(VoteID);
        }
    }
}

